package handling.login.handler;

import client.MapleClient;
import handling.channel.ChannelServer;
import handling.world.World;
import java.io.PrintStream;
import java.util.List;
import org.apache.mina.core.session.IoSession;
import server.ServerProperties;
import tools.FileoutputUtil;
import tools.HexTool;
import tools.data.LittleEndianAccessor;
import tools.packet.LoginPacket;

public class CharlistRequestHandler {

    public static void handlePacket(LittleEndianAccessor slea, MapleClient c) {
        if (!c.isLoggedIn()) {
            System.err.println("there 18");
            c.getSession().close(true);
            if (ServerProperties.IsLvkejian()) {
                String SendTo = "\r\n时间：" + FileoutputUtil.CurrentReadable_Time() + "  对玩家进行掉线处理 ----02 ";
                FileoutputUtil.packetLog("log\\玩家进行掉线处理.log", SendTo);
            }
            return;
        }
        slea.skip(1);
        int server = slea.readByte();
        int channel = slea.readByte() + 1;
        if ((!World.isChannelAvailable(channel)) || (server != 0)) {
            c.getSession().write(LoginPacket.getLoginFailed(10));
            return;
        }
        c.getSession().write(HexTool.getByteArrayFromHexString("AC 00"));
        String dl = "【客户地址】 " + c.getSession().getRemoteAddress().toString().split(":")[0] + " 连接到世界服务器：" + server + " 频道：" + channel;
        System.out.println(dl + "\r\n");

        List chars = c.loadCharacters(server);
        if ((chars != null) && (ChannelServer.getInstance(channel) != null)) {
            c.setWorld(server);
            c.setChannel(channel);
            //c.getSession().write(LoginPacket.EventCheck());
            c.getSession().write(LoginPacket.getCharList(c.getSecondPassword(), chars, c.getCharacterSlots()));
        } else {
            System.err.println("there 43");
            c.getSession().close(true);
            if (ServerProperties.IsLvkejian()) {
                String SendTo = "\r\n时间：" + FileoutputUtil.CurrentReadable_Time() + "  对玩家进行掉线处理 ----03 ";
                FileoutputUtil.packetLog("log\\玩家进行掉线处理.log", SendTo);
            }
        }
    }
}

